package com.mxp.elm.controller;

import com.mxp.elm.entity.LoginVo;
import com.mxp.elm.entity.UserEntity;
import com.mxp.elm.service.UserService;
import com.mxp.elm.utils.PageUtil;
import com.mxp.elm.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author mxp
 * @date 2023/2/23 11:38
 */
@RestController
@CrossOrigin
@RequestMapping("/user-info")
public class UserController {

    @Resource
    private UserService userService;

    @PutMapping
    public R addUser(@RequestBody UserEntity user) {
        return R.ok(userService.doSave(user));
    }

    @PutMapping("/register")
    public R register(@RequestBody UserEntity user) {
        return R.ok(userService.register(user));
    }

    @GetMapping("/list")
    public R getUserList(PageUtil page) {
        return R.ok(userService.queryPage(page));
    }

    @GetMapping("/{userId}")
    public R getUserById(@PathVariable("userId") Integer id) {
        return R.ok(userService.getUserById(id));
    }

    @PostMapping("/verify")
    public R login(@RequestBody LoginVo vo) {
        try {
            return R.ok(userService.verifyUser(vo));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping
    public R modify(@RequestBody UserEntity user) {
        return R.ok(userService.updateUserById(user));
    }

    @PostMapping("/{id}")
    public R modifyStatus(@PathVariable("id") Integer id) {
        return R.ok(userService.modifyUserStatusById(id));
    }

    @PostMapping("/resetPwd")
    public R resetPwd(@RequestBody UserEntity user) {
        return R.ok(userService.resetPwd(user));
    }
}
